package com.chunyu.ask.controller;
 

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.chunyu.ask.bean.Article;
import com.chunyu.ask.bean.Channel;
import com.chunyu.ask.service.ArticleService;
import com.chunyu.ask.service.ChannelService;
import com.chunyu.ask.util.PageUtils;
import com.chunyu.ask.util.RespMsg;

@Controller
@RequestMapping("/")
public class MainController { 
	@Autowired
	private ArticleService articleService;
	@Autowired
	private ChannelService channelService;
	/**
	 * 登陆QA触屏首页
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/")
	public ModelAndView forwordIndex(HttpServletRequest request,HttpServletResponse response){
		ModelAndView view = new ModelAndView(PageUtils.INDEX);
		Map<String, Object> paramMap = new HashMap<String, Object>();
		
		try {
			//所有——>健康咨询频道
			paramMap.put("firstResult", 0);
			paramMap.put("maxResult", PageUtils.MAX_PAGE);
			paramMap.put("parentId", PageUtils.CHANNEL_ID_JKZX);
			paramMap.put("isValid", 1);//有效
			paramMap.put("sortId", 1);//排序
			
			List<Channel> channelList= channelService.getList(paramMap);
			
			//健康资讯频道列表
			paramMap.clear();
			Channel  pindaoObj = channelService.getById(PageUtils.CHANNEL_ID_JKZXPD);
			if(pindaoObj != null){
				paramMap.put("firstResult", 0);
				paramMap.put("maxResult", PageUtils.MAX_PAGE);
				paramMap.put("parentId", pindaoObj.getId());
				List<Channel> pindaolList= channelService.getList(paramMap);
				view.addObject("pindaolList",pindaolList);
			}
			
			if(!CollectionUtils.isEmpty(channelList)){
				StringBuffer sbf = new StringBuffer("");
				//频道列表
				for(Channel obj:channelList){
					sbf.append(obj.getId()+",");
				}
				//轮播咨询列表
				if(!StringUtils.isEmpty(sbf)){
				String	channelIds = sbf.substring(0, sbf.length()-1);
				
					paramMap.put("firstResult", 0);
					paramMap.put("maxResult", PageUtils.NEWS_MAX_PAGE);
					paramMap.put("sortType", 1);//时间倒序
					paramMap.put("channelIds", channelIds);
					List<Article> articleList = articleService.getList(paramMap);
					view.addObject("articleList",articleList);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return view;
	}

	//###########Android * iOS 
	
	/**
	 * 获取：首页底部-健康咨询列表
	 * @return
	 */
	@RequestMapping("/bottomNewsList")
	public @ResponseBody RespMsg bottomNewsList() {
		RespMsg result = new RespMsg();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		try {
			//所有——>健康咨询频道
			paramMap.put("firstResult", 0);
			paramMap.put("maxResult", PageUtils.MAX_PAGE);
			paramMap.put("parentId", PageUtils.CHANNEL_ID_JKZX);
			paramMap.put("isValid", 1);//有效
			paramMap.put("sortId", 1);//排序
			
			List<Channel> channelList= channelService.getList(paramMap);
			
			
			if(!CollectionUtils.isEmpty(channelList)){
				StringBuffer sbf = new StringBuffer("");
				//频道列表
				for(Channel obj:channelList){
					sbf.append(obj.getId()+",");
				}
				//轮播咨询列表
				if(!StringUtils.isEmpty(sbf)){
				String	channelIds = sbf.substring(0, sbf.length()-1);
					paramMap.put("firstResult", 0);
					paramMap.put("maxResult", PageUtils.NEWS_MAX_PAGE);
					paramMap.put("sortType", 1);//时间倒序
					paramMap.put("channelIds", channelIds);
					List<Article> bottomNewsList = articleService.getList(paramMap);
					resultMap.put("bottomNewsList",bottomNewsList);
					result.resultObj=resultMap;
					result.status = true;
				}
			}
		} catch (Exception e) {
			result.status = false;
			result.msg="加载首页健康咨询列表异常";
			e.printStackTrace();
		}
		return result;
	}
	
	@RequestMapping("/channel")
	public ModelAndView demo(HttpServletRequest request,HttpServletResponse response){
		ModelAndView view = new ModelAndView("/channel/channel");
		return view;
	}
}
